package com.example.dllo.moondog.information.exercise;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.dllo.moondog.R;
import com.example.dllo.moondog.base.BaseFragment;
import com.example.dllo.moondog.information.strategy.StrategyActivity;
import com.example.dllo.moondog.tools.ReadStringRequestUtf;
import com.example.dllo.moondog.tools.VolleySingleton;
import com.google.gson.Gson;

/**
 * Created by dllo on 16/3/2.
 */
public class ExerciseFragment extends BaseFragment{
    private ListView listView;
    private ExerciseAdapter adapter;
    private String url = "http://qt.qq.com/static/pages/news/phone/c9_list_1.shtml";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exercise,null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView) getActivity().findViewById(R.id.listView_exercise);
        adapter = new ExerciseAdapter(getActivity());

        ReadStringRequestUtf requestUtf = new ReadStringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                final ExerciseBeans exerciseBeans = gson.fromJson(response, ExerciseBeans.class);
                adapter.addData(exerciseBeans.getList());
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getActivity(), ExerciseActivity.class);
                        intent.putExtra("url_exe", exerciseBeans.getList().get(position).getArticle_url());
                        startActivityForResult(intent, 200);
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        VolleySingleton.getInstance()._addRequest(requestUtf);
    }
}
